﻿package com.dp.service.jdc;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dp.pojo.News;
import com.dp.pojo.PageCtrl;
import com.dp.service.impl3.BaseImpl;

@Service
@Transactional
public class SearchServiceImpl extends BaseImpl {	
	/**
	 * 收集数据库的新闻信息
	 */
	public List<News> searchNews(String serchText,int pageSize,int pageNo){
	    int resultLimtBegin =  pageSize*pageNo;
		
		List<News> listnews = new ArrayList<News>(); 

		this.sql="select nc.contenttitle,nc.cid,nc.newstype,nc.contentid,nc.updatetime,nc.contenticon from news_contenttitle nc "+
			      "where nc.contenttitle like'%"+serchText+"%' order by nc.updatetime desc limit "+resultLimtBegin+","+pageSize;

		News news = null;
		try {
			List<Object[]> temp=this.em.createNativeQuery(sql).getResultList();
			for(Object[] o:temp)
            {
                news = new News();
                news.setContentTitle(this.getString(o[0]));
                news.setCId(this.getString(o[1]));
				news.setContentType(this.getString(o[2]));
                news.setContentId(this.getString(o[3]));
                news.setCreatedDate(this.getString(o[4]));
                news.setContentIcon(this.getString(o[5]));
                
                listnews.add(news);
            }
		} catch (Exception e) {
			logger.error(" SearchNews fail !!!" +e.getMessage());
			e.printStackTrace();
		}
		return listnews;
	}
	public PageCtrl getSerachPageCtrl(String serchText,int pageNo,int pageSize){
        PageCtrl pageCtrl = new PageCtrl();
        String sql = "select count(*) from news_contenttitle nc "+
        "where nc.contenttitle like'%"+serchText+"%'";
        try{
            int resultCount = Integer.valueOf( this.em.createNativeQuery(sql).getSingleResult().toString());
            pageCtrl.setResultCount(resultCount);
            pageCtrl.setPageNo(pageNo);
            pageCtrl.setPageSize(pageSize);
            pageCtrl.setPageCount(resultCount%pageSize==0?resultCount/pageSize:resultCount/pageSize+1);
            return pageCtrl;
            
        }catch (Exception e) {
            logger.error(" ChannelServiceImpl3-->>getPageCtrl fail !!!" +e.getMessage());
            e.printStackTrace();
            return null;
        }
    }
	 /**根据图片新闻Cid获取图片的Rid
     * @param newsCId
     * @return
     */
    public int getImgRid(String newsCId){
        //可以在这设置默认的Rid
        int rid =0;
        this.sql="select nc.rid from news_contenresource nc " +
                "where  nc.cid="+newsCId;
        try{
            
            List<Object> temp=this.em.createNativeQuery(sql).getResultList();
            rid = Integer.parseInt(this.getString(temp.get(0)));
        }catch (Exception e) {
            logger.error("IndexServiceImpl-->>getImgRid fail !!!" + e.getMessage());
            e.printStackTrace();
        }
        return rid;
        
    }
    /**根据图片新闻的Cid获取图片新闻的多个图片地址
     * @param newsCId
     * @return
     */
    public List<String> getImg(String newsCId){
        List<String> imgList= new ArrayList<String>();
        this.sql="select nr.fullpath from news_resources nr,news_contenresource nc " +
        		"where nr.rid=nc.rid and nc.cid="+newsCId;
        try{
            
            List<Object> temp=this.em.createNativeQuery(sql).getResultList();
            for(Object o:temp){
                imgList.add(this.getString(o));
            }
        }catch (Exception e) {
            logger.error("IndexServiceImpl-->>getImg fail !!!" + e.getMessage());
            e.printStackTrace();
        }
        return imgList;
        
    }
    /**根据图片文件路径,更改成-p_240x320.png路径
     * @return
     */
    public String changeFullPath(String fullPath){
        if(fullPath!=null&&fullPath.length()!=0){
            int length = fullPath.length()-4;
            String[] fileEnd =  {"_240x320.png","_320x480.png","_800x400.png","_800x400.jpg","_480x800.jpg","_320x480.jpg","_240x320.jpg","_174x192.jpg"};
            File file;;
            int i=0;
            do{
                fullPath = fullPath.substring(0,length);
                fullPath+=fileEnd[i].toString();
                file=new File(fullPath);
                i++;
            }while((!file.exists()&&i<fileEnd.length));
            if(!file.exists()){
                fullPath="opt\\242_upload\\2013\\05\\09\\1\\hnl2hghy7xdz.jpg";
            }
        }else {
            fullPath="opt\\242_upload\\2013\\05\\09\\1\\hnl2hghy7xdz.jpg";
        }
        return fullPath;
    }
	
	}

